/**
 * 
 */
package com.youtills.core.lang;

import java.security.Key;
import java.util.*;

/**
 * @author ravi
 *
 */
public class StringUtil {

    public static final String EMPTY = "";
    public static final String DOT = ".";
    public static final String COMMA = ",";
    public static final String QUESTION = "?";
    public static final String SEMICOLON = ";";
    public static final String COLON = ":";
    public static final String UNDER_SCORE = "_";
    public static final String HYPHEN = "-";
    public static final String TILDA = "~";
    public static final String PIPE = "|";

    public static boolean isEmpty(String text){
		return (text == null || text.trim().length() < 1);
	}

    public static String toString(Object[] obj){
        return toString(obj, ",");
    }

    public static String toString(Object[] obj, String delimiter){
        return toString(Arrays.asList(obj), delimiter);
    }

    public static String toString(Object obj){

        if(obj instanceof Collection){
            return toString(((Collection) obj), ",");
        }

        if(obj instanceof Map){
            return toString(((Map) obj), "=", ",");
        }

        return String.valueOf(obj);

    }

    public static String toString(Map<Object, Object> map, String keyValueSeparator, String delimiter){

        if(map != null){
            StringBuilder str = new StringBuilder();
            Set<Object> keys = map.keySet();
            boolean needDelimiter = false;
            if(keys != null && keys.size() > 0){
                for(Object key : keys){
                    Object value = map.get(key);
                    if(needDelimiter){
                        str.append(delimiter);
                    }else {
                        needDelimiter = true;
                    }
                    str.append(toString(key));
                    str.append(keyValueSeparator);
                    str.append(toString(value));
                }
            }
            return str.toString();
        }

        return String.valueOf(map);
    }

    public static <E> String toString(Collection<E> collection, String delimiter){
        if(collection != null){
            StringBuilder sb = new StringBuilder();
            boolean  needDelimiter = false;
            for(E ob : collection){
                if(needDelimiter){
                    sb.append(delimiter);
                }else{
                    needDelimiter = true;
                }
                sb.append(toString(ob));
            }
            return sb.toString();
        }
        return null;
    }


    public static Number getNumber(Object number){
        return getNumber(number, null);
    }

    public static Number getNumber(Object number, Number defaultValue){
        Number returnValue = defaultValue;
        if(number != null){
            try{
                returnValue = Double.parseDouble(toString(number));
            }catch(NumberFormatException nfe){
                //IGNORE
            }
        }

        return returnValue;
    }

    public static Boolean getBoolean(Object obj){
        return getBoolean(obj, null);
    }
    public static Boolean getBoolean(Object obj, Boolean defaultValue){
        Boolean returnValue = defaultValue;
        if(obj != null){
            if(obj instanceof Boolean){
                return (Boolean)obj;
            }
            String v = String.valueOf(obj);
            returnValue = v.equalsIgnoreCase("true") || v.equalsIgnoreCase("yes") || v.equalsIgnoreCase("y");
        }

        return returnValue;
    }

    public static String firstNonEmpty(Object... objects) {
        String returnValue = null;

        if(objects != null){
            for(Object obj : objects){
                if(obj != null){
                    String val = toString(obj);
                    if(val != null){
                        returnValue = val;
                        if(val.length() > 0){
                            break;
                        }
                    }
                }
            }
        }
        return returnValue;
    }

    public static List<String> parseTokens(String src, String delimiter) {
        return parseTokens(src, delimiter, false);
    }
    public static List<String> parseTokens(String src, String delimiter, boolean returnDelims) {
        if(src == null){
            return null;
        }
        ArrayList<String> tokens = new ArrayList<String>();
        StringTokenizer tokenizer = new StringTokenizer(src, delimiter, returnDelims);
        while(tokenizer.hasMoreElements()){
            tokens.add(tokenizer.nextToken());
        }

        return tokens;
    }
}
